package net.sf.calibur.engine.task;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class Handler {
	private String name;
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private Map<Object, Task> allTasks = new ConcurrentHashMap<Object, Task>();

	public Handler(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public Map<Object, Task> getAllTasks() {
		return allTasks;
	}

	public void addTask(Task task) {
		Object eventId = task.getEventId();
		if (allTasks.containsKey(eventId)) {
			throw new RuntimeException("Duplicated responsed event ID "
					+ eventId + " in task group " + getName());
		} else {
			task.setParent(this);
			allTasks.put(eventId, task);
		}
	}

	public abstract void prepareExecute(TaskInstance taskInstance);
	public abstract void executeTask(TaskInstance taskInstance);
	public abstract void postExecute(TaskInstance taskInstance);
}
